<template>
    <div class="type-nav">
<!-- 添加mouseleave事件在鼠标移出整个typeNav后二三级隐藏 -->
        <div class="container" @mouseleave="containerLeave"  @mouseenter="currentIndex=-1">
            <h2 class="all" @mouseenter="allEnter" >全部商品分类</h2>
            <nav class="nav">
                <a href="###">服装城</a>
                <a href="###">美妆馆</a>
                <a href="###">尚品汇超市</a>
                <a href="###">全球购</a>
                <a href="###">闪购</a>
                <a href="###">团购</a>
                <a href="###">有趣</a>
                <a href="###">秒杀</a>
            </nav>
            <!--使用showFlag控制一级分类的显示和隐藏-->
            <div class="sort" v-show="showFlag">
                <div class="all-sort-list2">
                    <!-- 一级 -->
                    <div class="item"
                         v-for="(item1,item1Index) in baseCategoryList"
                         :key="item1.categoryId"
                         :class="{showitemlist:currentIndex === item1Index}"
                         @click="toSearch"
                         @mouseenter="item1Enter(item1Index)">
                        <!-- 移入一级标题时,应让currentIndex和当前的itemIndex相同 添加mouseenter事件-->
                        <h3>
                            <a href="javascript:;"
                               :data-category1Id = item1.categoryId
                               :data-categoryName = item1.categoryName
                            >
                                {{item1.categoryName}}
                            </a>
                        </h3>
                        <div class="item-list clearfix">
                            <!-- 二级 -->
                            <div class="subitem"
                                 v-for="item2 in item1.categoryChild"
                                 :key="item2.categoryId">
                                <dl class="fore">
                                    <dt>
                                        <a href="javascript:;"
                                           :data-category2Id = item2.categoryId
                                           :data-categoryName = item2.categoryName
                                        >{{ item2.categoryName}}</a>
                                    </dt>
                                    <dd>
                                        <!-- 三级 -->
                                        <em v-for="item3 in item2.categoryChild"
                                            :key="item3.categoryId">
                                            <a href="javascript:;"
                                               :data-category3Id = item3.categoryId
                                               :data-categoryName = item3.categoryName
                                              >
                                                {{item3.categoryName}}
                                            </a>
                                        </em>

                                    </dd>
                                </dl>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {mapState} from "vuex";
//将typeNav注册为全局组件

//引入lodash中的节流函数
import throttle from "lodash/throttle";
export default {
    name: "typeNav",
    data(){
        return{
            currentIndex:-2,
            //如果地址包含"/" 或者 "/home" 就为true 显示
            showFlag:["/","/home"].includes(this.$route.path.toLowerCase())
        }
    },
    computed:{
        ...mapState({
            //state是总仓库
            baseCategoryList:state=>state.home.baseCategoryList
        })
    },
    methods:{
        //节流函数
        item1Enter:throttle(function (item1Index){
            //只有当 this.currentIndex > -2 才允许切换

            if(this.currentIndex > -2){
                this.currentIndex = item1Index
            }
        },200),
        //点击二三级分类跳转到search页面,并将分类的数据作为query带到search中
        toSearch(ev){
            console.log(ev.target.dataset)
            let {
                //dataset中的所有属性名最后都会变成全小写
                categoryname:categoryName,
                category1id:category1Id,
                category2id:category2Id,
                category3id:category3Id} = ev.target.dataset;
            //let {categoryName:categoryName,category1Id:category1Id,category2Id:category2Id,category3Id:category3Id} = ev.target.dataset
            console.log(categoryName,category1Id,category2Id,category3Id)
            let location = {name:"search"};
            let query = {};
            //将数据为空的query剔除掉
            categoryName !== undefined ? query.categoryName=categoryName + "": ""  ;
            category1Id !== undefined ?  query.category1Id=category1Id + "" : "" ;
            category2Id !== undefined ?  query.category2Id=category2Id + "" : "" ;
            category3Id !== undefined ?  query.category3Id=category3Id + "" : "" ;
            location.query = query;
            //如果已经存在params,就把整个params带上
            let params = this.$route.params;
            if(Object.keys(params).length !==0){
                //说明params有值
                location.params = params;
            }

            this.$router.push(location);
        },
        //控制一级分类的显示与隐藏的切换 如果在主页不允许切换
        containerLeave(){
            //将之前的currentIndex = -2放到containerLeave方法中
            this.currentIndex = -2;
            if ( ["/","/home"].includes(this.$route.path.toLowerCase())){
                return;
            }
            this.showFlag = false;
        },
        allEnter(){
           if ( ["/","/home"].includes(this.$route.path.toLowerCase())){
               return;
           }
           this.showFlag = true;
        }
    }

}
</script>

<style scoped lang="less">
.type-nav {
    border-bottom: 2px solid #e1251b;
    .container {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        position: relative;
        .all {
            width: 210px;
            height: 45px;
            background-color: #e1251b;
            line-height: 45px;
            text-align: center;
            color: #fff;
            font-size: 14px;
            font-weight: bold;
        }

        .nav {
            a {
                height: 45px;
                margin: 0 22px;
                line-height: 45px;
                font-size: 16px;
                color: #333;
            }
        }

        .sort {
            position: absolute;
            left: 0;
            top: 45px;
            width: 210px;
            height: 461px;
            position: absolute;
            background: #fafafa;
            z-index: 999;

            .all-sort-list2 {
                .item {
                    h3 {
                        line-height: 27px;
                        font-size: 14px;
                        font-weight: 400;
                        overflow: hidden;
                        padding: 0 20px;
                        margin: 0;

                        a {
                            color: #333;
                        }
                    }
                    .item-list {
                        display: none;
                        position: absolute;
                        width: 734px;
                        min-height: 460px;
                        background: #f7f7f7;
                        left: 210px;
                        border: 1px solid #ddd;
                        top: 0;
                        z-index: 9999 !important;

                        .subitem {
                            float: left;
                            width: 650px;
                            padding: 0 4px 0 8px;

                            dl {
                                border-top: 1px solid #eee;
                                padding: 6px 0;
                                overflow: hidden;
                                zoom: 1;

                                &.fore {
                                    border-top: 0;
                                }

                                dt {
                                    float: left;
                                    width: 54px;
                                    line-height: 22px;
                                    text-align: right;
                                    padding: 3px 6px 0 0;
                                    font-weight: 700;
                                }

                                dd {
                                    float: left;
                                    width: 415px;
                                    padding: 3px 0 0;
                                    overflow: hidden;

                                    em {
                                        float: left;
                                        height: 14px;
                                        line-height: 14px;
                                        padding: 0 8px;
                                        margin-top: 5px;
                                        border-left: 1px solid #ccc;
                                    }
                                }
                            }
                        }
                    }
                    //使用js控制显示隐藏不用hover
                    &.showitemlist {
                        .item-list {
                            display: block;
                        }
                    }
                }
            }
        }
    }
}
</style>